<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单Id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单Id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品序列号" prop="goodsSn">
      <el-input v-model="dataForm.goodsSn" placeholder="商品序列号"></el-input>
    </el-form-item>
    <el-form-item label="产品Id" prop="productId">
      <el-input v-model="dataForm.productId" placeholder="产品Id"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="number">
      <el-input v-model="dataForm.number" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="市场价" prop="marketPrice">
      <el-input v-model="dataForm.marketPrice" placeholder="市场价"></el-input>
    </el-form-item>
    <el-form-item label="零售价格" prop="retailPrice">
      <el-input v-model="dataForm.retailPrice" placeholder="零售价格"></el-input>
    </el-form-item>
    <el-form-item label="商品规格详情" prop="goodsSpecifitionNameValue">
      <el-input v-model="dataForm.goodsSpecifitionNameValue" placeholder="商品规格详情"></el-input>
    </el-form-item>
    <el-form-item label="虚拟商品" prop="isReal">
      <el-input v-model="dataForm.isReal" placeholder="虚拟商品"></el-input>
    </el-form-item>
    <el-form-item label="商品规格Ids" prop="goodsSpecifitionIds">
      <el-input v-model="dataForm.goodsSpecifitionIds" placeholder="商品规格Ids"></el-input>
    </el-form-item>
    <el-form-item label="图片链接" prop="listPicUrl">
      <el-input v-model="dataForm.listPicUrl" placeholder="图片链接"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderId: '',
          goodsId: '',
          goodsName: '',
          goodsSn: '',
          productId: '',
          number: '',
          marketPrice: '',
          retailPrice: '',
          goodsSpecifitionNameValue: '',
          isReal: '',
          goodsSpecifitionIds: '',
          listPicUrl: ''
        },
        dataRule: {
          orderId: [
            { required: true, message: '订单Id不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          goodsSn: [
            { required: true, message: '商品序列号不能为空', trigger: 'blur' }
          ],
          productId: [
            { required: true, message: '产品Id不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          marketPrice: [
            { required: true, message: '市场价不能为空', trigger: 'blur' }
          ],
          retailPrice: [
            { required: true, message: '零售价格不能为空', trigger: 'blur' }
          ],
          goodsSpecifitionNameValue: [
            { required: true, message: '商品规格详情不能为空', trigger: 'blur' }
          ],
          isReal: [
            { required: true, message: '虚拟商品不能为空', trigger: 'blur' }
          ],
          goodsSpecifitionIds: [
            { required: true, message: '商品规格Ids不能为空', trigger: 'blur' }
          ],
          listPicUrl: [
            { required: true, message: '图片链接不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/platform/ordergoods/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderId = data.ordergoods.orderId
                this.dataForm.goodsId = data.ordergoods.goodsId
                this.dataForm.goodsName = data.ordergoods.goodsName
                this.dataForm.goodsSn = data.ordergoods.goodsSn
                this.dataForm.productId = data.ordergoods.productId
                this.dataForm.number = data.ordergoods.number
                this.dataForm.marketPrice = data.ordergoods.marketPrice
                this.dataForm.retailPrice = data.ordergoods.retailPrice
                this.dataForm.goodsSpecifitionNameValue = data.ordergoods.goodsSpecifitionNameValue
                this.dataForm.isReal = data.ordergoods.isReal
                this.dataForm.goodsSpecifitionIds = data.ordergoods.goodsSpecifitionIds
                this.dataForm.listPicUrl = data.ordergoods.listPicUrl
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/platform/ordergoods/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderId': this.dataForm.orderId,
                'goodsId': this.dataForm.goodsId,
                'goodsName': this.dataForm.goodsName,
                'goodsSn': this.dataForm.goodsSn,
                'productId': this.dataForm.productId,
                'number': this.dataForm.number,
                'marketPrice': this.dataForm.marketPrice,
                'retailPrice': this.dataForm.retailPrice,
                'goodsSpecifitionNameValue': this.dataForm.goodsSpecifitionNameValue,
                'isReal': this.dataForm.isReal,
                'goodsSpecifitionIds': this.dataForm.goodsSpecifitionIds,
                'listPicUrl': this.dataForm.listPicUrl
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
